﻿using System.Drawing;
using System.Windows.Forms;

namespace Borg.Framework
{
    public class Preferences
    {
        private readonly string _extName;
        private readonly IPreferences _prefs;

        public Preferences(PlugEventArgs args)
        {
            _prefs = (IPreferences)args.PlugInfo.Object;
            _extName = args.SlotInfo.ExtensionInfo.Name;
        }

        public Preferences(IPreferences obj, string extName)
        {
            _prefs = obj;
            _extName = extName;
        }

        public object Get(string key, object defValue)
        {
            return (Extension[string.Format("{0}.{1}", _extName, key)] ?? defValue);
        }

        public void RestoreWindow(Form form)
        {
            var loc = _prefs[_extName + ".Location"];

            if (loc != null)
                form.Location = (Point) loc;

            var sz = _prefs[_extName + ".Size"];

            if (sz != null)
                form.Size = (Size) sz;
        }

        public void SaveWindow(Form form)
        {
            if (form.Location.X < 0 || form.Location.Y < 0)
                return;

            _prefs[_extName + ".Location"] = form.Location;
            _prefs[_extName + ".Size"]     = form.Size;
        }

        public void Set(string key, object value)
        {
            Extension[string.Format("{0}.{1}", _extName, key)] = value;
        }

        public IPreferences Extension
        {
            get { return _prefs; }
        }
    }
}

